package com.example.httpclient.UI;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.httpclient.Adapter.RecycleAdapter;
import com.example.httpclient.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;

public class DataShowActivity extends AppCompatActivity {

    private TextView tv_title;
    private RecyclerView recyclerView;
    private static ArrayList<Bitmap> Bitmaps = new ArrayList<>();
    private static ArrayList<Bitmap> BitmapTemps = new ArrayList<>();
    private final static String FileDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
    private final static String PicDir = "Crawler" + File.separator + "picture" + File.separator;
    private final static String path = FileDir + PicDir;
    private static RecycleAdapter adapter;
    private Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x00:
                    Bitmaps = (ArrayList<Bitmap>) msg.obj;
                    break;
                case 0x01:
                    BitmapTemps = Bitmaps;
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_show);
        initView();
        ArrayList<Bitmap> BitmapList = new ArrayList<>();
        File fileAll = new File(path);
        File[] files = fileAll.listFiles();
        if (files.length > 0) {
            for (File file : files) {
                if (String.valueOf(file.toPath()).endsWith(".jpg") || String.valueOf(file.toPath()).endsWith(".png") || String.valueOf(file.toPath()).endsWith(".jpeg")) {
                    try {
                        InputStream inputStream = new FileInputStream(String.valueOf(file.toPath()));
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        BitmapList.add(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        tv_title.setText("图片显示");
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        adapter=new RecycleAdapter(this,BitmapList);
        recyclerView.setAdapter(adapter);
    }


    private void initView() {
        tv_title=findViewById(R.id.tV_title);
        recyclerView=findViewById(R.id.rec);
    }

}